package com.eleadmin.store.biz.impl;

import com.eleadmin.damon.utils.constant.UserConstant;
import com.eleadmin.damon.utils.results.BaseDao;
import com.eleadmin.damon.utils.results.PageResult2;

import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.common.system.entity.User;
import com.eleadmin.store.biz.SellSonBiz;
import com.eleadmin.store.entity.EntryBillSon;
import com.eleadmin.store.entity.SellSon;
import com.eleadmin.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2024-03-07 14:15:36
 * @version ：1.0
 */
@Repository
public class SellSonBizImpl extends BaseDao implements SellSonBiz {

    @Override
    public JsonResult list(PageResult2<T> page, SellSon sellSon, User loginUser) throws Exception {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<SellSon> rm = new BeanPropertyRowMapper<SellSon>(SellSon.class);
        StringBuffer sql = new StringBuffer("select s.id,s.sellId,s.organization_id");
        sql.append(",s.warehouseName,s.productName,s.specification,s.lengths");
		sql.append(",s.negative,s.num,s.liWeight,s.liTotalWeight");
		sql.append(",s.actualWeight,s.price,s.totalPrice");
		sql.append(",s.model,s.locality,s.create_time,s.create_user");
		sql.append(",s.update_time,s.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName,suser2.nickname updateName ");
        sql.append(" from we_sell_son s ");
        sql.append(" inner join sys_user suser on suser.user_id=s.create_user ");
        sql.append(" left join sys_user suser2 on suser2.user_id=s.update_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=s.organization_id and sogzn.deleted=0 ");
        sql.append(" where s.deleted=0 and s.sellId=:sellId ");
        map.put("sellId", sellSon.getSellid() + "");
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and s.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if(Utils.isNotEmpty(sellSon)){
    //			if(Utils.isNotEmpty(sellSon.getName())){
    //				sql.append(" and instr(s.name,:name)>0 ");
    //				map.put("name", sellSon.getName());
    //			}
            if(Utils.isNotEmpty(sellSon.getSort())&&Utils.isNotEmpty(sellSon.getOrder())){
                sql.append(" order by s."+sellSon.getSort()+" "+sellSon.getOrder());
            } else {
                sql.append(" order by s.create_time asc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<SellSon> list(SellSon sellSon, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<SellSon> rm = new BeanPropertyRowMapper<SellSon>(SellSon.class);
        StringBuffer sql = new StringBuffer("select s.id,s.sellId,s.organization_id");
        sql.append(",s.warehouseName,s.productName,s.specification,s.lengths");
		sql.append(",s.negative,s.num,s.liWeight,s.liTotalWeight");
		sql.append(",s.actualWeight,s.price,s.totalPrice");
		sql.append(",s.model,s.locality,s.create_time,s.create_user");
		sql.append(",s.update_time,s.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from we_sell_son s ");
        sql.append(" inner join sys_user suser on suser.user_id=s.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=s.organization_id and sogzn.deleted=0 ");
        sql.append(" where s.deleted=0 and s.sellId=:sellId ");
        map.put("sellId", sellSon.getSellid() + "");
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and s.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if(Utils.isNotEmpty(sellSon)){
            if(Utils.isNotEmpty(sellSon.getSort())&&Utils.isNotEmpty(sellSon.getOrder())){
                sql.append(" order by s."+sellSon.getSort()+" "+sellSon.getOrder());
            } else {
                sql.append(" order by s.create_time asc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(),map, rm);
    }

    @Override
    public Double calcTotalPrice(SellSon sellSon, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select IFNULL(sum(s.totalPrice),0) totalPrice ");
        sql.append(" from we_sell_son s ");
        sql.append(" where s.deleted=0 and s.sellId=:sellId");
        map.put("sellId", sellSon.getSellid()+"");
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, Double.class);
    }

    @Override
    public SellSon getById(String id) {
        RowMapper<SellSon> rm = new BeanPropertyRowMapper<SellSon>(SellSon.class);
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select s.id,s.sellId,s.organization_id");
        sql.append(",s.warehouseName,s.productName,s.specification,s.lengths");
		sql.append(",s.negative,s.num,s.liWeight,s.liTotalWeight");
		sql.append(",s.actualWeight,s.price,s.totalPrice");
		sql.append(",s.model,s.locality,s.create_time,s.create_user");
		sql.append(",s.update_time,s.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from we_sell_son s ");
        sql.append(" inner join sys_user suser on suser.user_id=s.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=s.organization_id and sogzn.deleted=0 ");
        sql.append(" where s.deleted=0 and s.id=:id ");
        map.put("id", id);
        getMapParameter(sql, map);
        try {
            return jdbcTemplate.queryForObject(sql.toString(), map, rm);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public boolean save(SellSon sellSon, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("insert into we_sell_son (");
        sql.append("sellId,organization_id,warehouseName,productName");
		sql.append(",specification,lengths,negative,num");
		sql.append(",liWeight,liTotalWeight,actualWeight");
		sql.append(",price,totalPrice,model,locality");
		sql.append(",create_user,create_time) values ( ");
        sql.append(":sellId,:organization_id,:warehouseName,:productName");
		sql.append(",:specification,:lengths,:negative,:num");
		sql.append(",:liWeight,:liTotalWeight,:actualWeight");
		sql.append(",:price,:totalPrice,:model,:locality");
		sql.append(",:create_user,now())");
        map.put("sellId", sellSon.getSellid()==null?null:sellSon.getSellid()+"");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("warehouseName", sellSon.getWarehousename());
        map.put("productName", sellSon.getProductname());
        map.put("specification", sellSon.getSpecification());
        map.put("lengths", sellSon.getLengths());
        map.put("negative", sellSon.getNegative());
        map.put("num", sellSon.getNum()+"");
        map.put("liWeight", sellSon.getLiweight()+"");
        map.put("liTotalWeight", sellSon.getLitotalweight()+"");
        map.put("actualWeight", sellSon.getActualweight()+"");
        map.put("price", sellSon.getPrice()+"");
        map.put("totalPrice", sellSon.getTotalprice()+"");
        map.put("model", sellSon.getModel());
        map.put("locality", sellSon.getLocality());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(SellSon sellSon, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("update we_sell_son set ");
        sql.append("sellId=:sellId,warehouseName=:warehouseName,productName=:productName ");
		sql.append(",specification=:specification,lengths=:lengths,negative=:negative ");
		sql.append(",num=:num,liWeight=:liWeight,liTotalWeight=:liTotalWeight ");
		sql.append(",actualWeight=:actualWeight,price=:price ");
		sql.append(",totalPrice=:totalPrice,model=:model,locality=:locality ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
        map.put("sellId", sellSon.getSellid()==null?null:sellSon.getSellid()+"");
        map.put("warehouseName", sellSon.getWarehousename());
        map.put("productName", sellSon.getProductname());
        map.put("specification", sellSon.getSpecification());
        map.put("lengths", sellSon.getLengths());
        map.put("negative", sellSon.getNegative());
        map.put("num", sellSon.getNum()+"");
        map.put("liWeight", sellSon.getLiweight()+"");
        map.put("liTotalWeight", sellSon.getLitotalweight()+"");
        map.put("actualWeight", sellSon.getActualweight()+"");
        map.put("price", sellSon.getPrice()+"");
        map.put("totalPrice", sellSon.getTotalprice()+"");
        map.put("model", sellSon.getModel());
        map.put("locality", sellSon.getLocality());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", sellSon.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(String id, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if(id.length()>0){
            for(String id1 : id.split(",")){
                map  = new HashMap<String, String>();
                if(id1==null || id1.equals("")){ break; }
                StringBuffer sql =new StringBuffer("update we_sell_son set deleted=1" +
                    ",update_user=:update_user, update_time=now() "
                    + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", id1);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(),map);
            }
        }
        return count > 0;
    }
}
